﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RegistrationDate
{
    public class RegistCtl
    {
        /// <summary>
        /// 有效日期
        /// </summary>
        string _effectiveDate = "2023-07-15 23:59:59";
        /// <summary>
        /// 剩余有效总天数
        /// 如果安装时间超过有效日期，即此时间有效
        /// 单位：天
        /// </summary>
        int _remainingTotalDay = 30;
        /// <summary>
        /// 标志
        /// </summary>
        string _flag;
        /// <summary>
        /// 时间
        /// </summary>
        DateTime _dt;

        public int RemainingDays { get; set; }


        public RegistCtl() {
            DateTime dt = DateTime.Now;

            if(dt > Convert.ToDateTime(_effectiveDate)) {
                _effectiveDate = dt.AddDays(_remainingTotalDay).ToString("yyyy-MM-dd HH:mm:ss");
            }

            CRegEditCtl reg = new CRegEditCtl(dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), 0.ToString(),_effectiveDate);
            //先读
            string enStr = reg.ReadRegPath(reg.keyName);
            string datetime = AESModel.AESDecrypt(enStr);

            string enStr1 = reg.ReadRegFlag(reg.regKeyName);
            int regFlag = int.Parse( AESModel.AESDecrypt(enStr1));

            _effectiveDate = reg.ReadRegeditDe(reg.keyName, reg.effectiveDateName);

            //有效期结束，需要提示注册
            //先判断第一次启动时间是否大于有效时间
            if (Convert.ToDateTime(datetime) >= Convert.ToDateTime(_effectiveDate)) {//这里可能永远不会进来
                if(DateTime.Now > Convert.ToDateTime(_effectiveDate).AddDays(_remainingTotalDay)) {
                    //向注册列表写入需要注册的标志信息
                    reg.WriteRegeditEn(reg.keyName, reg.regKeyName, 1.ToString());
                }
            } else {
                if (DateTime.Now > Convert.ToDateTime(_effectiveDate)) {
                    //向注册列表写入需要注册的标志信息
                    reg.WriteRegeditEn(reg.keyName, reg.regKeyName, 1.ToString());
                }
            }

            UseTimeJudgment();
        }

        /// <summary>
        /// 注册判断
        /// 
        /// </summary>
        /// <returns>返回值为true时，即需要注册；返回值为false时，即不需要注册</returns>
        public bool RegisteredJudgment() {
            CRegEditCtl reg = new CRegEditCtl();

            //读标志
            string enStr1 = reg.ReadRegFlag(reg.regKeyName);
            int regFlag = int.Parse(AESModel.AESDecrypt(enStr1));

            if (regFlag == 0) return false;
            return true;
        }

        /// <summary>
        /// 使用时间判断
        /// </summary>
        /// <returns></returns>
        public void UseTimeJudgment() {
            CRegEditCtl reg = new CRegEditCtl();

            string effectiveDateEn = reg.ReadRegedit(reg.keyName, reg.effectiveDateName) as string;
            string effectiveDate = AESModel.AESDecrypt(effectiveDateEn);
            TimeSpan ts =  Convert.ToDateTime(effectiveDate) - DateTime.Now;

            RemainingDays =(int)ts.TotalDays;
        }

    }
}
